package com.nf.health.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.core.MyOnClickListener;
import com.nf.health.app.models.Person;
import com.nf.health.app.utils.ImageLoaderUtils;

import java.util.List;

public class FamilyListAdapter extends BaseAdapter {
	private Context context;
	private List<Person> datas;
	
	public FamilyListAdapter(Context context,List<Person> datas) {
		this.context = context;
		this.datas = datas;
	}

	@Override
	public int getCount() {
		int count = datas.size();
		if(count >0 ){
			count += 2;
		}else{
			count += 1;
		}
		return count;
	}

	@Override
	public Object getItem(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	
	class ViewHolder{
		ImageView iv_header;
		ImageView iv_delete;
		TextView tv_title;
	}
	
	boolean isShowDelete;
	public void setShowDelete(boolean isShowDelete){
		this.isShowDelete = isShowDelete;
		this.notifyDataSetChanged();
	}
	
	public boolean isShowDelete(){
		return isShowDelete;
		
	}

	
	
	MyOnClickListener listener;
    public void setMyOnClickListener(MyOnClickListener listener){
    	this.listener = listener;
    }

    @Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		ViewHolder holder;
//		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_family_person, null);
			holder = new ViewHolder();
			holder.iv_header = (ImageView) convertView.findViewById(R.id.iv_header);
			holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_name);
			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder)convertView.getTag();
//		}
		if(position < datas.size()){
			holder.tv_title.setText(datas.get(position).getUserid());
			holder.tv_title.setVisibility(View.VISIBLE);
			if(isShowDelete){
				holder.iv_delete.setVisibility(View.VISIBLE);
			}else{
				holder.iv_delete.setVisibility(View.GONE);
			}
		}else{
			holder.tv_title.setVisibility(View.INVISIBLE);
			holder.iv_delete.setVisibility(View.GONE);
		}
		
		if(position == datas.size()){
			holder.iv_header.setImageResource(R.drawable.add_person);
		}else if(position == datas.size()+1){
			holder.iv_header.setImageResource(R.drawable.delete_person);
		}else{
			ImageLoaderUtils.getinstance(context).getRoundedCornerBitmap(holder.iv_header,datas.get(position).getUserHimg(), R.drawable.default_head);
		}
		
		holder.iv_delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.onClick(v, 0, position, 0);
			}
		});
		
		return convertView;
	}
    
    

}
